//
//  WTPlayChatInputView.m
//  WTPlayDemo
//
//  Created by ben on 16/7/25.
//  Copyright © 2016年 ben. All rights reserved.
//

#import "WTPlayChatInputView.h"
#import "Masonry.h"
#import "UIColor+WTExtend.h"
#import "UIImage+WTExtend.h"

@interface WTPlayChatInputView ()<UITextFieldDelegate>{
    BOOL isDanmuOn;
}
@property (nonatomic, strong) UIView*   danmuBgView;
@property (nonatomic, strong) UIButton* danmuButton;
@property (nonatomic, strong) UITextField*  inputTextField;
@property (nonatomic, strong) UIButton*     sendButton;
@property (nonatomic, strong) UIButton*     faceButton;
@end

@implementation WTPlayChatInputView
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
    
        self.backgroundColor = [UIColor whiteColor];
        [self setup];
    }
    return self;
}

#pragma mark - 
- (void)setup{

   
    self.danmuBgView = ({
        
        UIView* view = [[UIView alloc] init];
        view.backgroundColor = [UIColor hexChangeFloat:@"dcdcdc"];
        view.layer.cornerRadius = 15;
        view.layer.masksToBounds = YES;
        [self addSubview:view];
        view;
    });
    
    self.danmuButton = ({
        UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
        [button setBackgroundImage:[UIImage wt_imageWithName:@"wt_play_danmu_off"] forState:UIControlStateNormal];
        [self.danmuBgView addSubview:button];
        button;
    });
    
    self.inputTextField = ({
        
        UITextField* field = [[UITextField alloc] init];
        field.returnKeyType = UIReturnKeySend;
        field.backgroundColor = [UIColor hexChangeFloat:@"EDEDED"];
        field.delegate = self;
        field.layer.cornerRadius = 8;
        field.layer.masksToBounds = YES;
        [self addSubview:field];
        field;
    });
    
    
    self.sendButton = ({
        
        UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
        [button setTitle:@"发送" forState:UIControlStateNormal];
        button.titleLabel.font = [UIFont systemFontOfSize:12];
        button.layer.cornerRadius = 8;
        button.layer.masksToBounds = YES;
        [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [button setBackgroundColor:[UIColor themeColor]];
        [self addSubview:button];
        button;
    });
    
    
    [self layout];
}

#pragma mark - 
- (void)layout{
    
    [self.danmuBgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(6);
        make.height.mas_equalTo(30);
        make.centerY.mas_equalTo(self.mas_centerY);
        make.width.mas_equalTo(60);
    }];
    
    [self.danmuButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(2);
        make.top.mas_equalTo(2);
        make.bottom.mas_equalTo(-2);
        make.width.mas_equalTo(45);
    }];
    
    
    [self.sendButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.mas_equalTo(-8);
        make.width.mas_equalTo(54);
        make.height.mas_equalTo(30);
        make.centerY.mas_equalTo(self.mas_centerY);
    }];
    
    
    [self.inputTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.danmuBgView.mas_trailing).offset(5);
        make.trailing.mas_equalTo(self.sendButton.mas_leading).offset(-5);
        make.height.mas_equalTo(30);
        make.centerY.mas_equalTo(self.mas_centerY);
    }];

}

#pragma mark - 
- (void)buttonPressed:(id)sender{

    if(sender == self.danmuButton){
        
        if(!isDanmuOn){
            [self.danmuButton setBackgroundImage:[UIImage wt_imageWithName:@"wt_play_danmu_on"] forState:UIControlStateNormal];
            [self.danmuButton mas_updateConstraints:^(MASConstraintMaker *make) {
                make.leading.mas_equalTo(13);
            }];
            self.inputTextField.placeholder = @"发送弹幕1个萝卜/条";
        }else {
            [self.danmuButton setBackgroundImage:[UIImage wt_imageWithName:@"wt_play_danmu_off"] forState:UIControlStateNormal];
            [self.danmuButton mas_updateConstraints:^(MASConstraintMaker *make) {
                make.leading.mas_equalTo(2);
            }];
            self.inputTextField.placeholder = nil;
        }
        
        [UIView animateWithDuration:0.25 animations:^{
            [self layoutIfNeeded];
        }];
        isDanmuOn = !isDanmuOn;
    }else if(sender == self.sendButton){
        if (self.block) {
            self.block(isDanmuOn,self.inputTextField.text);
        }
        self.inputTextField.text = nil;
    }else {
        
    }
}


#pragma mark - getter
- (NSString *)messageBody{
    return self.inputTextField.text;
}

- (void)setMessageBody:(NSString *)messageBody{
    
    self.inputTextField.text = messageBody;
}

- (void)popKeyBoard{
    
    [self.inputTextField becomeFirstResponder];
}

- (void)dismissKeyBoard{
    [self.inputTextField resignFirstResponder];
}

#pragma mark - 
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    
    if (self.block) {
        self.block(isDanmuOn,textField.text);
    }
    return YES;
}
@end
